package cn.hiapi.user.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.system.ILocationService;
import cn.hiapi.interfaces.system.data.LocationData;
import cn.hiapi.publics.vo.ShopBaseVo;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.entity.shop.ShopScore;
import cn.hiapi.shop.entity.shop.ShopType;
import cn.hiapi.shop.service.shop.ShopScoreService;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shop.service.shop.ShopTypeService;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.user.HistoryRecord;
import cn.hiapi.shopkeeper.service.activity.ActivityFactory;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import cn.hiapi.shopkeeper.service.user.HistoryRecordService;
import cn.hiapi.user.query.HistoryRecordQuery;
import cn.hiapi.user.vo.HistoryRecordV2Vo;
import cn.hiapi.user.vo.HistoryRecordVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/31 22:01
 */
@RestController
@RequestMapping("/user/history-record-v2")
public class HistoryRecordV2Controller extends BasicQueryController<HistoryRecord, Long, HistoryRecordV2Vo, HistoryRecordQuery> {
    private final HistoryRecordService basicService;

    protected HistoryRecordV2Controller(HistoryRecordService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
        this.basicService = basicService;
    }


    @Override
    public HistoryRecordQuery setQuery(HistoryRecordQuery query) {
        query.setUid(TokenGet.get().getFid());
        query.setDirection(Sort.Direction.DESC);
        query.setProperties("id");
        return super.setQuery(query);
    }

    @Override
    public List<HistoryRecordV2Vo> toListVo(List<HistoryRecord> list, String[] fields) {
        return super.toList(list, fields).stream().filter(e -> e != null).collect(Collectors.toList());
    }

    @Override
    public HistoryRecordV2Vo toData(HistoryRecord data, String[] fields) {
        if (data.getShopId() == 0) return null;
        Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(data.getShopId());
        if (shop == null) return null;
        HistoryRecordV2Vo vo = shop.copy(new HistoryRecordV2Vo());
        vo.setHistoryId(data.getId());
        ShopType type = this.dispatchContext.getServiceOne(ShopTypeService.class).get(shop.getCid());
        if (type != null)
            vo.setCategory(type.getName());
        StringBuffer address = new StringBuffer();
        List<LocationData> locationDataList = this.dispatchContext.getServiceOne(ILocationService.class).locationGet(shop.getStateId(), shop.getCityId(), shop.getDistrictId());
        locationDataList.forEach(e -> address.append(e.getName()).append(" "));
        address.append(shop.getAddress());
        vo.setAddress(address.toString());
        ShopScore score = this.dispatchContext.getServiceOne(ShopScoreService.class).getByShopId(0, shop.getId());
        if (score != null)
            vo.setScore(score.getComplexScore());
        return vo;
    }


    @ApiOperation(value = "删除全部足迹", notes = "删除全部足迹", nickname = "AdinZ")
    @PostMapping("/delete-all")
    public ResponseEntity deleteAll() {
        this.basicService.deleteAll(TokenGet.get().getFid());
        return toSuccess();
    }


    @ApiOperation(value = "删除指定", notes = "删除指定", nickname = "AdinZ")
    @PostMapping("/delete")
    public ResponseEntity delete(@RequestParam("id") Long[] id) {
        this.basicService.deleteByUid(TokenGet.get().getFid(), id);
        return toSuccess();
    }


}
